package com.qire.manhua.model.viewModel.impl;

import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.BaseAdapter;

import com.qire.antsrouter.lifecycleHandler.ActivityManageHandler;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.BuyMembershipDialogController;
import com.qire.manhua.dal.service.BuyMembershipDialogService;
import com.qire.manhua.databinding.LayoutBuyMembershipDialogPackageItemBinding;
import com.qire.manhua.dialog.BuyResultTipsDialog;
import com.qire.manhua.dialog.SpecialEventDialog;
import com.qire.manhua.model.enumeration.PaymentMethod;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;

public class BuyMembershipDialogViewModel extends BaseViewModel implements IViewModel {

    private BuyMembershipDialogService buyMembershipService = new BuyMembershipDialogService(this);

    private final ArrayList<MembersPackageItemViewModel> membersPackageList = new ArrayList<>();
    private final SummerBindAdapter membersPackageAdapter = new SummerBindAdapter(membersPackageList);

    private MembersPackageItemViewModel selectPackageItem;

    @Override
    public int getVariableId() {
        return BR.buyMembershipDialogViewModel;
    }

    //region    #附加管理 begin
    @Override
    protected void onCleared() {
        super.onCleared();
        this.buyMembershipService = null;
        this.membersPackageAdapter.setEventProcess(null);
    }

    public void onAddition(BuyMembershipDialogController controller) {
        this.membersPackageAdapter.setEventProcess(controller);
        refreshBuyButtonUI();
    }
    //endregion #附加管理 end


    //region    #数据与业务 begin
    private void toggleSelectItem(MembersPackageItemViewModel itemViewModel) {
        if(ObjectUtil.nonNull(selectPackageItem)) {
            selectPackageItem.setHaveChosen(false);
        }

        if(ObjectUtil.notEquals(selectPackageItem, itemViewModel)) {
            selectPackageItem = itemViewModel;
            itemViewModel.setHaveChosen(true);
        } else {
            selectPackageItem = null;
        }
        refreshBuyButtonUI();
    }

    public boolean haveChosen() {
        return ObjectUtil.nonNull(selectPackageItem);
    }

    public void loadPageData() {
        buyMembershipService.loadPageData();
    }

    public void clearMembersPackageList() {
        membersPackageList.clear();
    }
    public void addMembersPackageItem(String describe, String buyPrice, String originalPrice, int beanReward, String cornerLabel) {
        membersPackageList.add(new MembersPackageItemViewModel(describe, buyPrice, originalPrice, beanReward, cornerLabel));
    }
    public void refreshMembersPackageAdapter() {
        membersPackageAdapter.notifyDataSetChanged();
    }
    public void checkDefault() {
        if(ListUtil.notEmpty(membersPackageList)) {
            membersPackageList.get(0).toggle();
        }
    }

    public void buyBuyMembership(PaymentMethod paymentMethod, String formActivity) {
        buyMembershipService.buyBuyMembership(selectPackageItem, paymentMethod, formActivity);
    }
    //endregion #数据与业务 end

    //region    #Dialog管理 begin
    public void showBuyResult() {
        if(ObjectUtil.nonNull(selectPackageItem)) {
            Activity currentActivity = ActivityManageHandler.HANDLER.currentActivity();
            BuyResultTipsDialog tipsDialog = new BuyResultTipsDialog(currentActivity, BuyResultTipsDialog.PayTipType.Membership);
            tipsDialog.setOnDismissListener(dialog -> currentActivity.finish());
            tipsDialog.setMembershipDays(selectPackageItem.describe)
                    .show();
        }

    }

    public void showSpecialEvent(String imgUrl, String pageUrl) {
        Activity previousActivity = ActivityManageHandler.HANDLER.previousActivity();
        new SpecialEventDialog(previousActivity)
                .setImagePath(imgUrl)
                .setEventInfo(pageUrl, "").show();
    }
    //endregion #Dialog管理 end

    //region    #UI更新 begin
    private void refreshBuyButtonUI() {
        boolean haveChosen = ObjectUtil.nonNull(selectPackageItem);
        set("payButtonText", "立即支付");
        set("payButtonTextColor", haveChosen ? "#000000" : "#909090");
        set("payButtonBg", haveChosen ? R.drawable.app_buy_membership_button_yellow : R.drawable.app_buy_membership_button_gray);
    }
    //endregion #UI更新 end

    //region    #UI绑定 begin
    public MutableLiveData<CharSequence> getPayButtonText() {
        return get("payButtonText");
    }

    public MutableLiveData<String> getPayButtonTextColor() {
        return get("payButtonTextColor");
    }

    public MutableLiveData<Integer> getPayButtonBg() {
        return get("payButtonBg");
    }

    public BaseAdapter getMembersPackageAdapter() {
        return membersPackageAdapter;
    }
    //endregion #UI绑定 end


    @AdapterEntityBind(viewRid = R.layout.layout_buy_membership_dialog_package_item)
    public class MembersPackageItemViewModel extends BaseObservable implements AutoVariable<LayoutBuyMembershipDialogPackageItemBinding> {

        public final String describe;
        public final String buyPrice;
        public final String originalPrice;
        public final int    beanReward;
        public final String cornerLabel;

        private boolean haveChosen = false;

        public MembersPackageItemViewModel(String describe, String buyPrice, String originalPrice, int beanReward, String cornerLabel) {
            this.describe       = describe;
            this.buyPrice       = buyPrice;
            this.originalPrice  = originalPrice;
            this.beanReward     = beanReward;
            this.cornerLabel    = cornerLabel;
        }

        @Override
        public void bindVariable(LayoutBuyMembershipDialogPackageItemBinding viewBind) {
            viewBind.setMembersPackageItemViewModel(this);
        }

        public void setHaveChosen(boolean haveChosen) {
            this.haveChosen = haveChosen;
            notifyPropertyChanged(BR.haveChosen);
        }

        public void toggle() {
            toggleSelectItem(this);
        }

        @Bindable
        public boolean getHaveChosen() {
            return haveChosen;
        }

        @Bindable
        public CharSequence getDescribe() {
            return describe;
        }

        @Bindable
        public CharSequence getBuyPrice() {
            return buyPrice;
        }

        @Bindable
        public CharSequence getOriginalPrice() {
            SpannableString originalPriceBuilder = new SpannableString(originalPrice);
            originalPriceBuilder.setSpan(new StrikethroughSpan(), 3, originalPriceBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            return originalPriceBuilder;
        }

        @Bindable
        public int getBeanRewardVisible() {
            return beanReward <= 0 ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public CharSequence getBeanReward() {
            return "送" + beanReward + "金豆";
        }

        @Bindable
        public CharSequence getCornerLabel() {
            return cornerLabel;
        }
    }

}
